#coding: utf-8
import json
import io
import sys

# 执行方式 python test.py 2 3   
# themeIndex 皮肤列表的position
# appIndex 应用列表的position

themeIndex = sys.argv[1];
appIndex = sys.argv[2];
# print 'themeIndex:', themeIndex
# print 'appIndex:', appIndex

themeList = [
	["tuo51", "#FACB2F", "static/home_tab_assess_1.png", "static/home_tab_children_1.png", "static/home_tab_user_1.png"],
	["knsh", "#DD742C", "static/home_tab_assess_knsh.png", "static/home_tab_children_knsh.png", "static/home_tab_user_knsh.png"],
	["msdd", "#ABD1F5", "static/home_tab_assess_msdd.png", "static/home_tab_children_msdd.png", "static/home_tab_user_msdd.png"],
	["czcp", "#B5DB7D", "static/home_tab_assess_czcp.png", "static/home_tab_children_czcp.png", "static/home_tab_user_czcp.png"],
]

appList = [
	["wx0c213f104f1c231f", "托无忧测评", ""],
	["wx785122fc0f5d90fd", "小康轩托班测评系统", "kx"],
	["wx6d897a71f2f75579", "金职成长测评系统", "msdd"],
	["wx8033fe9de99c902d", "育优优测评", "yyy"],
	["wxf11d82eb1a38ad9e", "儿童成长测评系统", ""],
	["wxfcbfea348616744b", "儿童成长测评项目", "cpxm"],
]
themeReplace = themeList[int(themeIndex)];
appReplace = appList[int(appIndex)]

# 修改 uni.scss
with io.open('./uni.scss', 'r', encoding='UTF-8') as fp:
    jsonStr = fp.read();


for theme in themeList:
	jsonStr = jsonStr.replace("$theme-type:%s;"%theme[0], "$theme-type:%s;"%themeReplace[0])
	jsonStr = jsonStr.replace("$app-main-color: %s"%theme[1], "$app-main-color: %s"%themeReplace[1])
with open("./uni.scss", "w") as fp:
    fp.write(jsonStr.encode("utf-8"))


# 修改 App.vue
with io.open('./App.vue', 'r', encoding='UTF-8') as fp:
    jsonStr = fp.read();

for theme in themeList:
	jsonStr = jsonStr.replace("theme: '%s',"%theme[0], "theme: '%s',"%themeReplace[0])
	jsonStr = jsonStr.replace("themeColor: '%s',"%theme[1], "themeColor: '%s',"%themeReplace[1])
	
for app in appList:
	jsonStr = jsonStr.replace("appid: '%s',"%app[0], "appid: '%s',"%appReplace[0])
	jsonStr = jsonStr.replace("appName: '%s',"%app[1], "appName: '%s',"%appReplace[1])
	jsonStr = jsonStr.replace("channel: '%s',"%app[2], "channel: '%s',"%appReplace[2])

with open("./App.vue", "w") as fp:
    fp.write(jsonStr.encode("utf-8"))
	
	
	
# 修改 pages.json
with io.open('./pages.json', 'r', encoding='UTF-8') as fp:
    jsonStr = fp.read();
pages = json.loads(jsonStr)

navigationBarTitleText = appReplace[1]
selectedColor = themeReplace[1]
selectedIconPath1 = themeReplace[2]
selectedIconPath2 = themeReplace[3]
selectedIconPath3 = themeReplace[4]

pages["globalStyle"]["navigationBarTitleText"] = navigationBarTitleText.decode('utf-8')
pages["tabBar"]["selectedColor"] = selectedColor.decode('utf-8')
pages["tabBar"]["list"][0]["selectedIconPath"] = selectedIconPath1.decode('utf-8')
pages["tabBar"]["list"][1]["selectedIconPath"] = selectedIconPath2.decode('utf-8')
pages["tabBar"]["list"][2]["selectedIconPath"] = selectedIconPath3.decode('utf-8')

with open("./pages.json", "w") as fp:
    fp.write(json.dumps(pages, indent=4, ensure_ascii=False).encode("utf-8"))



# 修改 manifest.json
with io.open('./manifest.json', 'r' ,encoding='UTF-8') as fp:
    manifestStr = fp.read();
manifest = json.loads(manifestStr)

description = appReplace[1]
appid = appReplace[0]

manifest["description"] = description.decode('utf-8')
manifest["mp-weixin"]["appid"] = appid.decode('utf-8')

with open("./manifest.json", "w") as fp:
    fp.write(json.dumps(manifest, indent=4, ensure_ascii=False).encode("utf-8"))